package mybatis;


import com.littlenb.mybatisjpa.keygen.IdentityKeyGenerator;

import com.littlenb.mybatisjpa.support.Constant;
import com.littlenb.mybatisjpa.support.MybatisJapConfiguration;
import javax.annotation.PostConstruct;

import com.maas.util.mybatis.jpa.littlenb.common.db.JbaDbPluginConst;
import com.maas.util.mybatis.jpa.littlenb.support.MyAnnotationStatementRegistry;
import com.maas.util.mybatis.jpa.littlenb.support.AnnotationStatementScanner;
import org.apache.ibatis.executor.keygen.KeyGenerator;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.base.springmvc.plugin.DbPluginConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author sway.li
 **/
@Component
public class MybatisJpaInitializer {

  @Autowired
  private SqlSessionFactory sqlSessionFactory;

  @PostConstruct
  public void init() {

      //注意@Mapper 对应的实体类 要加 @Id
    Configuration configuration = sqlSessionFactory.getConfiguration();
    KeyGenerator keyGenerator = new IdentityKeyGenerator(new MyIdGenerator());
    configuration.addKeyGenerator(Constant.DEFAULT_KEY_GENERATOR, keyGenerator);

    JbaDbPluginConst.DIALECT_CURR = DbPluginConst.DIALECT_CURR;
    /**
     * 扫描 @Mapper @Entity
     * 可以修改扫描的注解
     */
    AnnotationStatementScanner annotationStatementScanner = new AnnotationStatementScanner.Builder()
        .configuration(configuration)
        .basePackages(new String[]{"com.xxx.mapper"})
        //.annotationStatementRegistry(AnnotationStatementRegistry.getDefaultRegistry()).build();
            .entityPackageArr(new String[]{"com.xxx.entity","com.maas.util.mybatis.jpa.littlenb.mapper"})
       .annotationStatementRegistry(MyAnnotationStatementRegistry.getDefaultRegistry()).build();

    annotationStatementScanner.scanEntity();
    annotationStatementScanner.scan();

  }

  public static void conf() {
    boolean isLower =false;
    if(isLower){
      MybatisJapConfiguration conf = MybatisJapConfiguration.getInstance();
      conf.setCamelToUnderline(true);
      conf.setTableCaseMode(Constant.LOWER_CASE_MODE);
      conf.setColumnCaseMode(Constant.LOWER_CASE_MODE);
    }

  }

}
